﻿using UnityEngine;
using System.Collections;
using System.IO;


public class CProfileMgr
{
    //초기화 함수
    public bool Initialize()
    {
        if (false == ReadUserName())
        {
            if (RuntimePlatform.Android == Application.platform ||
           RuntimePlatform.IPhonePlayer == Application.platform)
            {
                TouchScreenKeyboard Keyboard;
                Keyboard = TouchScreenKeyboard.Open("UserName", TouchScreenKeyboardType.Default);
                while (TouchScreenKeyboard.visible == false)
                {
                    new WaitForSeconds(1.0f);
                    Debug.Log("Keyboard is active?: " + Keyboard.active);
                    Debug.Log("Keyboard is done?: " + Keyboard.done);
                }
                m_strUserName = Keyboard.text;
            }
            WriteUserName();
            Debug.Log("UserName : " + m_strUserName.ToString());
        }
        m_Ranking.ReadRankingFile();
        return true;
    }

    public void Terminate()
    {
        m_Ranking.WriteRankingFile();
    }

    public bool WriteUserName()
    {
        FileStream fUserName;
        fUserName = CGlobalMgr.Instance.FileMgr.OpenFile(GlobalDefine.g_strProfileFileName, FileAccess.Write);
        if (null == fUserName)
            return false;

		BinaryWriter writer = new BinaryWriter(fUserName, System.Text.Encoding.UTF8);
        if (null != writer)
        {
			writer.Write(m_strUserName);
            writer.Close();
        }
        CGlobalMgr.Instance.FileMgr.CloseFile(fUserName);


        return true;
    }

    public bool ReadUserName()
    {
        if (false == CGlobalMgr.Instance.FileMgr.ExistsFile(GlobalDefine.g_strProfileFileName))
        {
            Debug.LogError("Not ExistsFile" + Application.persistentDataPath.ToString() + GlobalDefine.g_strProfileFileName.ToString());
            return false;
        }

        FileStream fUserName;
        fUserName = CGlobalMgr.Instance.FileMgr.OpenFile(GlobalDefine.g_strProfileFileName, FileAccess.Read);

		BinaryReader reader = new BinaryReader(fUserName, System.Text.Encoding.UTF8);
        if (null != reader)
        {
			m_strUserName = reader.ReadString();
            reader.Close();
        }
        CGlobalMgr.Instance.FileMgr.CloseFile(fUserName);
        return true;
    }

	private string m_strUserName;
	private CRanking m_Ranking;

	public string strUserName
	{
		get { return m_strUserName; }
	}
}
